define(['jquery', 'bootstrap', 'backend', 'table', 'form','sortable'], function ($, undefined, Backend, Table, Form,sortable) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/gather/index' + location.search,
                    add_url: 'wechat/gather/add',
                    edit_url: 'wechat/gather/edit',
                    del_url: 'wechat/gather/del',
                    multi_url: 'wechat/gather/multi',
                    import_url: 'wechat/gather/import'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: '来源',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = "<a href=" + row.link + " target='_blank'>" + row.title + "</a>";

                                return result;
                            }
                        },
                        {
                            field: 'cat_name',
                            title: '分类',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'author',
                            title: '作者',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'addtime',
                            title: '添加时间',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'imgs_count',
                            title: '图片数量',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                return row.imgs ? row.imgs.split(',').length : 0;
                            }
                        },
                        {
                            field: 'article_id',
                            title: '是否采集',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row) {
                                var result = row.article_id &&  "<a href='/panel.php/wechat/articles/edit/ids/" + row.article_id + "' target='_blank'>" + row.article_id + "</a>";

                                return result;
                            }
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 批量推送
            $(document).on("click", "#btn-push", function () {
                var ids = Table.api.selectedids(table, true);
                // var   selectedData = Table.api.selecteddata(table, true);
                var index = layer.load();
                $.ajax({
                    url: "wechat/gather/push",
                    type: 'post',
                    dataType: 'json',
                    data: {ids: ids.join(',')},
                    success: function (ret) {
                        console.log('ret', ret)
                        ret.code === 0 && Backend.api.toastr.error(ret.msg);
                        ret.code === 1 && Backend.api.toastr.success(ret.msg);

                        layer.close(index);
                        if(ret.code === 1){
                            table.bootstrapTable('refresh');
                        }


                    }, error: function (e) {
                        Backend.api.toastr.error(e.msg);
                        layer.close(index);
                    }
                });
            });

            $(document).on("click", "#btn-update-catId", function () {

                var index = layer.load();
                $.ajax({
                    url: "wechat/gather/updateGatherCatId",
                    type: 'post',
                    dataType: 'json',
                    success: function (ret) {
                        table.bootstrapTable('refresh');


                    }, error: function (e) {
                        Backend.api.toastr.error(e.msg);
                        layer.close(index);
                    }
                });
            });
            $( "#sortable" ).sortable();
            // $(document).on("click", "#test", function () {
            //
            //     $( "#sortable" ).sortable();
            // });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
